package service

import (
	"errors"
	"wms/internal/model"
	"wms/pkg/database"
)

// LogisticsService 物流服务
type LogisticsService struct{}

// NewLogisticsService 创建物流服务
func NewLogisticsService() *LogisticsService {
	return &LogisticsService{}
}

// GetLogisticsCompanyList 获取物流公司列表
func (s *LogisticsService) GetLogisticsCompanyList(keyword string, status, level, page, pageSize int) ([]model.LogisticsCompany, int64, error) {
	db := database.GetDB()
	var companies []model.LogisticsCompany
	var total int64

	// 构建查询
	query := db.Model(&model.LogisticsCompany{})

	// 根据条件过滤
	if keyword != "" {
		query = query.Where("code LIKE ? OR name LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
	}
	if status > 0 {
		query = query.Where("status = ?", status)
	}
	if level > 0 {
		query = query.Where("level = ?", level)
	}

	// 获取总数
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 分页查询
	offset := (page - 1) * pageSize
	if err := query.Offset(offset).Limit(pageSize).Order("id ASC").Find(&companies).Error; err != nil {
		return nil, 0, err
	}

	return companies, total, nil
}

// GetLogisticsCompanyByID 根据ID获取物流公司
func (s *LogisticsService) GetLogisticsCompanyByID(id uint) (*model.LogisticsCompany, error) {
	var company model.LogisticsCompany
	if err := database.GetDB().First(&company, id).Error; err != nil {
		return nil, err
	}
	return &company, nil
}

// CreateLogisticsCompany 创建物流公司
func (s *LogisticsService) CreateLogisticsCompany(company *model.LogisticsCompany) error {
	// 检查物流公司编码是否已存在
	var count int64
	if err := database.GetDB().Model(&model.LogisticsCompany{}).Where("code = ?", company.Code).Count(&count).Error; err != nil {
		return err
	}
	if count > 0 {
		return errors.New("物流公司编码已存在")
	}

	// 创建物流公司
	return database.GetDB().Create(company).Error
}

// UpdateLogisticsCompany 更新物流公司
func (s *LogisticsService) UpdateLogisticsCompany(company *model.LogisticsCompany) error {
	// 检查物流公司是否存在
	var existingCompany model.LogisticsCompany
	if err := database.GetDB().First(&existingCompany, company.ID).Error; err != nil {
		return errors.New("物流公司不存在")
	}

	// 检查物流公司编码是否重复（如果有修改）
	if company.Code != existingCompany.Code {
		var count int64
		if err := database.GetDB().Model(&model.LogisticsCompany{}).Where("code = ? AND id != ?", company.Code, company.ID).Count(&count).Error; err != nil {
			return err
		}
		if count > 0 {
			return errors.New("物流公司编码已存在")
		}
	}

	// 更新物流公司，忽略CreatedAt字段
	return database.GetDB().Model(company).Omit("created_at").Updates(company).Error
}

// DeleteLogisticsCompany 删除物流公司
func (s *LogisticsService) DeleteLogisticsCompany(id uint) error {
	// 检查物流公司是否存在
	var company model.LogisticsCompany
	if err := database.GetDB().First(&company, id).Error; err != nil {
		return errors.New("物流公司不存在")
	}

	// 删除物流公司
	return database.GetDB().Delete(&company).Error
}

// BatchDeleteLogisticsCompany 批量删除物流公司
func (s *LogisticsService) BatchDeleteLogisticsCompany(ids []uint) error {
	return database.GetDB().Where("id IN ?", ids).Delete(&model.LogisticsCompany{}).Error
} 